package com.fancy.framework
{
	import flash.utils.Dictionary;

	public class BundleManager
	{
		protected static var _instance:BundleManager;
		public static function get instance():BundleManager
		{
			if(!_instance) { _instance = new BundleManager(); }
			return _instance;
		}
		
		private var _leaderDic:Dictionary = new Dictionary();
		
		public function BundleManager()
		{
		}
		
		public function addBundle(leader:IBundleLeader):IBundleLeader
		{
			_leaderDic[leader.getModuleName()] = leader;
			return leader;
		}
		
		public function getLeaderByModuleName(name:String):IBundleLeader
		{
			return _leaderDic[name];
		}
		
		public function initAllBundles():void
		{
			var LeaderVec:Vector.<IBundleLeader> = new Vector.<IBundleLeader>();
			for(var key:* in _leaderDic)
			{
				LeaderVec.push( IBundleLeader(_leaderDic[key]) );
			}
			LeaderVec.sort(SortOnLeaderLevel);
			for(var i:int = 0; i < LeaderVec.length; i++)
			{
				LeaderVec[i].initializeLeader();
			}
		}
		private function SortOnLeaderLevel(a:IBundleLeader, b:IBundleLeader):int
		{
			if(a.getLevel() > b.getLevel()) { return 1; }
			else if(a.getLevel() < b.getLevel()) { return -1; }
			else { return 0; }
		}
		
	}
}